package cn.javabb.core.plugin.thymeleaf;

import cn.javabb.core.plugin.thymeleaf.util.ConfigUtil;
import cn.javabb.core.plugin.thymeleaf.util.NavUtil;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.expression.IExpressionObjectFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class BbsExpressionObjectFactory implements IExpressionObjectFactory {

    public static final String NAV_UTIL_NAME = "navUtil";
    public static final NavUtil NAV_UTIL_OBJECT = new NavUtil();

    public static final String CONFIG_UTIL_NAME = "configUtil";
    public static final ConfigUtil CONFIG_UTIL_OBJECT = new ConfigUtil();

    @Override
    public Set<String> getAllExpressionObjectNames() {
        // 有多个，继续往里面添加
        Set<String> names = Collections.unmodifiableSet(new LinkedHashSet<String>(Arrays.asList(
                NAV_UTIL_NAME,
                CONFIG_UTIL_NAME
        )));
        return names;
    }

    @Override
    public Object buildObject(IExpressionContext iExpressionContext, String objectName) {
        if(NAV_UTIL_NAME.equals(objectName)){
            return NAV_UTIL_OBJECT;
        }
        if(CONFIG_UTIL_NAME.equals(objectName)){
            return CONFIG_UTIL_OBJECT;
        }
        // 有多个，继续往下面if
        return null;
    }

    @Override
    public boolean isCacheable(String objectName) {
        return objectName!=null;
    }
}
